<?php

namespace App\Controller\App;

use App\Base\BaseController4Restful;
use App\Repository\AnnounceRepository;
use App\Repository\CategoryRepository;
use App\Repository\ProductRepository;
use Symfony\Component\Routing\Annotation\Route;
use function Doctrine\ORM\QueryBuilder;

class IndexController extends BaseController4Restful
{
    /**
     * @Route("/app/index", name="AppIndexData")
     */
    public function AppIndexData(CategoryRepository $categoryRepository ,ProductRepository $repository)
    {
        //获取行业分类中首页置顶的项目
        $cats = $categoryRepository->findBy(array("isTop"=>1,"enable"=>1));
        $catData = [];
        foreach ($cats as $cat) {
            $qb = $repository->createQueryBuilder("A");
            $qb->select("A");
            $qb->where("A.cat1 = {$cat->getId()}");
            $qb->andWhere("A.status > 199");
            $qb->orderBy("A.isIndex","DESC");
            $qb->addOrderBy("A.id","DESC");
            $qb->setMaxResults(3);
            $qb->setFirstResult(0);
            $list = $qb->getQuery()->execute();
            $catData[]=[
                "cat"=>$cat,
                "list"=>$list,
            ];
        }
        return $this->json_out(200,"OK",$catData);
    }
    /**
     * @Route("/app/announce", name="AppAnnounceList")
     */
    public function AppAnnounceList(AnnounceRepository $repository)
    {
        if(!$event = $this->getParamRequired("event")){$event = 0;}
        if ($event) {
            $data = $repository->getByEventId($event);
        }else{
           $data = $repository->findBy(array(),array("id"=>"DESC"),10,0);
        }

        $this->setGroups("app");

        return $this->json_out(200,"数据获取成功",$data);
    }
//    /**
//     * @Route("/app/announce", name="AppAnnounceList")
//     */
//    public function AppAnnounceList(AnnounceRepository $repository)
//    {
//        if(!$event = $this->getParamRequired("event")){return $this->json_out(400,"参数缺失");}
//        if (!is_array($event)) {
//            $event = json_decode($event,true);
//        }
//        $data = [];
//        $minLength = 10;
//        foreach ($event as $key => $eventId) {
//            $data[$key] = $repository->getByEventId($eventId);
//            if (count($data[$key]) < $minLength) {
//                $minLength = count($data[$key]);
//            }
//        }
//        if ($minLength == 0) {
//            return $this->json_out(200,"OK",[]);
//        }
//
//        foreach ($data as $key => &$row) {
//            $row = array_splice($row,0,$minLength);
//        }
//
//        $this->setGroups("app");
//
//        return $this->json_out(200,"数据获取成功",$data);
//    }
}
